#!/bin/bash

export HIVE_HOME=/usr/bin/hive

if [ $# == 1 ]

then
   dateStr=$1
   
else
   dateStr=`date -d '-1 day' +'%Y-%m-%d'`
  

fi


yearStr=`date -d ${dateStr} +'%Y'`
month_for_quarter=`date -d ${dateStr} +%-m`
quarterStr=$((($month_for_quarter-1)/3+1))
monthStr=`date -d ${dateStr} +'%m'`
dayStr=`date -d ${dateStr} +'%d'`

echo ${yearStr}
echo ${quarterStr}
echo ${monthStr}
echo ${dayStr}

hiveSql="
	--动态分区配置
set hive.exec.dynamic.partition=true;
set hive.exec.dynamic.partition.mode=nonstrict;
--hive压缩
set hive.exec.compress.intermediate=true;
set hive.exec.compress.output=true;
--写入时压缩生效
set hive.exec.orc.compression.strategy=COMPRESSION;
--矢量化查询(批量读取操作)
set hive.vectorized.execution.enabled=true; 

set hive.auto.convert.join=true;
-- smb
set hive.enforce.bucketing=true; 
set hive.enforce.sorting=true; 
set hive.optimize.bucketmapjoin = true;
set hive.auto.convert.sortmerge.join=true;
set hive.auto.convert.sortmerge.join.noconditionaltask=true;
set hive.optimize.bucketmapjoin.sortedmerge = true;

insert overwrite table zx_dwb.dwd_web_chat_ems partition(yearinfo,quarterinfo,monthinfo,dayinfo)
select
    wce.id, 
    wce.create_date_time, 
    wce.session_id, 
    wce.sid, 
    wce.create_time, 
    wce.seo_source, 
    wce.seo_keywords, 
    wce.ip, 
    wce.area, 
    wce.country, 
    wce.province, 
    wce.city, 
    wce.origin_channel, 
    wce.user1, 
    wce.manual_time, 
    wce.begin_time, 
    wce.end_time, 
    wce.last_customer_msg_time_stamp, 
    wce.last_agent_msg_time_stamp, 
    wce.reply_msg_count, 
    wce.msg_count, 
    wce.browser_name, 
    wce.os_info, 
    wce.hourinfo,
    wcte.referrer,
    wcte.from_url,
    wcte.landing_page_url,
    wcte.url_title,
    wcte.platform_description,
    wcte.other_params,
    wcte.history,
    wce.yearinfo,
    wce.quarterinfo,
    wce.monthinfo,
    wce.dayinfo
from (select * from zx_dwd.dwd_web_chat_ems where yearinfo='${yearStr}' and quarterinfo='${quarterStr}' and monthinfo='${monthStr}' and dayinfo='${dayStr}') wce  join (select * from zx_dwd.dwd_web_chat_text_ems where dt = '${dateStr}') wcte on wce.id = wcte.id  ;
"


${HIVE_HOME} -S -e "${hiveSql}"